const lottery = {
    lotteryRandom: getRequests().lotteryRandom,
    lotteryPrizesAll: getRequests().lotteryPrizesAll,
    lotteryList: getRequests().lotteryList,
    getMyPrizes: getRequests().getMyPrizes,
    lotteryGetTimes: getRequests().lotteryGetTimes,
    getUserCoins: getRequests().getUserCoins,
    token: getRequests().token,
    roomId: bridge.call("room.getRoomId", null) || getRequests().roomId,
    choujiangList = null,
    newLists: [],
    myPrizeJiLu: [],
    rowId: getQueryString('rowId') || 6,
    count: 0, //位置总数
    speed: 10, //初始速度
    timer: 0, //定时器时间
    currentIndex: -1, //转动的当前位置
    rotateNum: 0, //转动次数
    basicCycle: 50, //运动初始次数
    prizePlace: -1, //中奖位置
    isClick: false, //是否重复点击抽奖
    parentDom: null, //根dom
    initOppo: 1, //初始抽奖次数
    quanzi: { "data": [{ "addMoney": 1, "category": "类目7", "cost": 30, "giftId": "Z56128", "giftName": "波棒糖", "id": 27, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/056d46ab628d4f45a146ccb095010614.png", "index": 0 }, { "addMoney": 1, "category": "类目8", "cost": 50, "giftId": "Z95278", "giftName": "音你而在", "id": 28, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/2c076f0c7b294571acc1562064474a38.png", "index": 1 }, { "addMoney": 1, "category": "类目9", "cost": 50, "giftId": "Z59784", "giftName": "项链", "id": 29, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/85c30c723dc945ebb432c6cbbe480e16.png", "index": 2 }, { "addMoney": 1, "category": "不中奖的礼物", "cost": 100, "giftId": "Z20008", "giftName": "耳环", "id": 30, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/1907ca29056747849641f8cb5677ea72.png", "index": 3 }, { "addMoney": 1, "category": "类目12", "cost": 200, "giftId": "Z65381", "giftName": "黄金叶", "id": 32, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/3150063572ea4b0fb77c9f88140cbbec.png", "index": 4 }, { "addMoney": 1, "category": "类目13", "cost": 300, "giftId": "Z77027", "giftName": "爱心猪", "id": 33, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/321604a16b234848ab546e85c1724b26.png", "index": 11 }], "data2": [{ "addMoney": 1, "category": "类目14", "cost": 500, "giftId": "Z71201", "giftName": "Papty", "id": 37, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/6d4c45b0630a42c88dcfc510faeaaba2.png", "index": 5 }, { "addMoney": 1, "category": "类目15", "cost": 2000, "giftId": "Z90897", "giftName": "TT", "id": 38, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/a014ce7aa8ae49d8ab854fc17b97359b.png", "index": 10 }, { "addMoney": 1, "category": "类目16", "cost": 10000, "giftId": "Z53936", "giftName": "粑粑", "id": 39, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/0aa2b45f08ef458eac1e8daa02779c03.png", "index": 9 }, { "addMoney": 1, "category": "类目18", "cost": 52000, "giftId": "Z86182", "giftName": "心动", "id": 40, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/7ebe46bbb4514a6dbe005e646b55cce1.png", "index": 8 }, { "addMoney": 1, "category": "不中奖的礼物", "cost": 131400, "giftId": "Z96247", "giftName": "表白弹幕", "id": 41, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/5d02dd63830046c19aeb114e20c04c47.jpg", "index": 7 }, { "addMoney": 1, "category": "类目17", "cost": 200000, "giftId": "Z90003", "giftName": "告白气球", "id": 44, "img": "http://kaile-yunban.oss-cn-shenzhen.aliyuncs.com/images/add30fd1368744ca9d65c0a58e56d0e7.png", "index": 6 }] },
    trChooseImgHtml: function() {
        var html = template('showChooseImgs', this.quanzi);
        $('.trChooseImg').html(html);
    },
    ajax() {
        function AjaxRequest(opts) {
            this.type = opts.type || "get";
            this.url = opts.url;
            this.param = opts.param || {};
            this.isShowLoader = opts.isShowLoader || false;
            this.dataType = opts.dataType || "json";
            this.callBack = opts.callBack;
            this.
            this.init();
        }

        AjaxRequest.prototype = {
            //初始化
            init: function() {
                this.sendRequest();
            },
            //渲染loader
            showLoader: function() {
                if (this.isShowLoader) {
                    var loader = '<div class="ajaxLoader"><div class="loader">加载中...</div></div>';
                    $("body").append(loader);
                }
            },
            //隐藏loader
            hideLoader: function() {
                if (this.isShowLoader) {
                    $(".ajaxLoader").remove();
                }
            },
            //发送请求
            sendRequest: function() {
                var self = this;
                $.ajax({
                    type: this.type,
                    url: this.url,
                    data: this.param,
                    dataType: this.dataType,
                    beforeSend: this.showLoader(),
                    success: function(res) {
                        self.hideLoader();
                        if (res != null && res != "") {
                            if (self.callBack) {
                                if (Object.prototype.toString.call(self.callBack) === "[object Function]") { //Object.prototype.toString.call方法--精确判断对象的类型
                                    self.callBack(res);
                                } else {
                                    console.log("callBack is not a function");
                                }
                            }
                        }
                    }
                });
            }
        };

        window.AjaxRequest = AjaxRequest;
    },
    init() {
        const _this = this;
        this.trChooseImgHtml();
        const initOppo = document.getElementById('resOpportunity');
        if ($('#lotteryBox').find('li').length > 0) {
            $lottery = $('#lotteryBox');
            $units = $lottery.find('li');
            this.count = $units.length;
            this.parentDom = $lottery;
        }
        // if (this.initOppo > 0) {
        $('#lotteryBox .lotteryBtn').on('click', function() {
                if (_this.isClick) { //防止在转动过程中,重复点击抽奖按钮
                    // alert('你已用完抽奖次数')
                    return false
                } else {
                    _this.speed = 100;
                    _this.rotateNum = 0;

                    // _this.initOppo -= 1;
                    // initOppo.innerHTML = '还有' + _this.initOppo + '次机会'
                    _this.turning();
                    _this.isClick = true; //一次完成后，可继续抽
                    return false;
                }
            })
            // }
    },
    addNextItemClass() {
        this.parentDom.find('.lottery-unit-' + this.currentIndex).removeClass("active");
        this.currentIndex += 1;
        if (this.currentIndex >= this.count - 1) {
            this.currentIndex = 0;
        }
        this.parentDom.find('.lottery-unit-' + this.currentIndex).addClass('active');
        return false
    },
    turning() {
        this.rotateNum += 1;
        // console.log(this)
        this.addNextItemClass();
        if (this.rotateNum > this.basicCycle + 10 && this.prizePlace == this.currentIndex) {
            clearTimeout(this.timer)
            this.prizePlace = -1;
            this.timer = 0;
            this.initOppo != 0 ? this.isClick = false : this.isClick = true;
            let selectedEle = $(this.parentDom.find('.lottery-unit-' + lottery.currentIndex).selector)[0].dataset.value;
            // console.log("恭喜你中了" + selectedEle + "等奖");
        } else {
            if (this.rotateNum < this.basicCycle) {
                this.speed -= 10
            } else if (this.rotateNum == this.basicCycle) {
                this.prizePlace = Math.floor(Math.random() * this.count);
                this.prizePlace == 8 ? this.prizePlace = 0 : this.prizePlace = this.prizePlace;
                // console.log(this.prizePlace)
            } else {
                if (this.rotateNum > this.basicCycle + 10 && ((this.prizePlace == 0 && this.currentIndex == 7)) || this.prizePlace == this.currentIndex + 1) {
                    this.speed += 100;
                } else {
                    this.speed += 20;
                }
            }
            if (lottery.speed < 40) {
                lottery.speed = 40;
            };
            this.timer = setTimeout(this.turning.bind(this), this.speed) //此处使用bind(),防止setTimeout改变this的指向
        }
        return false;
    }
}
window.onload = () => {
    lottery.init();
}